package com.dingwen.payment.controller;


import com.dingwen.commons.entity.Payment;
import com.dingwen.payment.service.IPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dingwen
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {

    private final IPaymentService paymentService;

    @Value("${server.port}")
    private Integer port;

    /**
     * 发现客户端
     */
    private final DiscoveryClient discoveryClient;

    @Autowired
    public PaymentController(IPaymentService paymentService, DiscoveryClient discoveryClient) {
        this.paymentService = paymentService;
        this.discoveryClient = discoveryClient;
    }

    /**
     * 保存
     *
     * @param payment 付款
     * @return {@link Object}
     */
    @PostMapping("")
    public Object save(@RequestBody Payment payment) {
        return paymentService.save(payment);
    }


    /**
     * 找到
     *
     * @param id id
     * @return {@link Object}
     */
    @GetMapping("/{id}")
    public Object find(@PathVariable Long id) {
        System.out.println("port = " + port);
        return paymentService.getById(id);
    }

    /**
     * 服务发现
     *
     * @return {@link Object}
     */
    @GetMapping("/discoveryClient")
    public Object find() {
        // 服务列表
        List<String> services = discoveryClient.getServices();
        services.forEach(System.out::println);
        // 服务实例列表
        List<ServiceInstance> instanceList = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instanceList.forEach(System.out::println);
        return discoveryClient;
    }

    @GetMapping("/rest/test")
    public Object restTemplateGetForEntityTest(){
        return paymentService.getById(1);

    }

}
